//
//  KSDTabBarViewController.m
//  KSuodun
//
//  Created by DwZeng on 2022/7/18.
//

#import "KSDTabBarViewController.h"
#import "KSDTabBar.h"
@interface KSDTabBarViewController ()<UITabBarControllerDelegate>
/** 当前选中的 item 的索引 */
@property (assign ,nonatomic) NSInteger currentSelectedIndex;

@end

@implementation KSDTabBarViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTabBarColor];
    
    self.delegate = self;
        
    KSDHomeController *riceController = [[KSDHomeController alloc] init];
    [self addChildVc:riceController title:@"首页" image:@"" selectedImage:@""];

    KSDShoppingMallController *collectionController = [[KSDShoppingMallController alloc] init];
    [self addChildVc:collectionController title:@"商城" image:@"" selectedImage:@""];

    KSDOnlineServiceController *noteController = [[KSDOnlineServiceController alloc] init];
    [self addChildVc:noteController title:@"客服" image:@"" selectedImage:@""];

    KSDMainController *mineController = [[KSDMainController alloc] init];
    [self addChildVc:mineController title:@"我的" image:@"" selectedImage:@""];
        
}

/**
 *  添加一个子控制器
 *
 *  @param childVc       子控制器
 *  @param title         标题
 *  @param image         图片
 *  @param selectedImage 选中的图片
 */
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置子控制器的文字
    childVc.title = title; // 同时设置tabbar和navigationBar的文字
    
    // 设置子控制器的图片
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 设置文字的样式
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    // 先给外面传进来的小控制器 包装 一个导航控制器
    KSDNavigationController *nav = [[KSDNavigationController alloc] initWithRootViewController:childVc];
    // 添加为子控制器
    [self addChildViewController:nav];
}


#pragma - mark  UITabBarController代理方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    
    self.currentSelectedIndex = tabBarController.selectedIndex;
}

/**
 重写tabbar
 */
- (void)setupTabBarColor {
    
    KSDTabBar *tabBar = [[KSDTabBar alloc] init];
    tabBar.delegate = self;
    [self setValue:tabBar forKeyPath:@"tabBar"];

    [tabBar setBackgroundImage:[self imageWithColor:[UIColor clearColor]]];
    [tabBar setShadowImage:[self imageWithColor:[UIColor clearColor]]];
    //     背景图颜色
    [[UITabBar appearance] setBarTintColor:[UIColor clearColor]];
    [UITabBar appearance].translucent = YES;

    // 设置一个自定义 View,大小等于 tabBar 的大小
    UIView *tabbarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, tabBarHeight)];
    UIImageView *topLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.4)];
    topLine.backgroundColor = [UIColor whiteColor];
    [tabbarView addSubview:topLine];
    // 给自定义 View 设置颜色
    tabbarView.backgroundColor = [UIColor whiteColor];
    
    // 将自定义 View 添加到 tabBar 上
    [tabBar insertSubview:tabbarView atIndex:0];
}

- (UIImage *)imageWithColor:(UIColor *)color {
    
    CGRect rect = CGRectMake(0.0f,0.0f, 1.0f,1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context =UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


- (void)dealloc{
    
}
@end
